// miniprogram/pages/mine/setting/typeManage/typeManage.js
const app = getApp()
const util = require('../../../../utils/util.js')
const common = require('../../../../utils/common.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFullSucreen: app.globalData.isFullSucreen,
    slideButtons: [{
      type: 'warn',
      text: '删除'
    }],
    type: '',
    typeList: [],
    user:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if (options.type) {
      switch (options.type) {
        case 'member':
          wx.setNavigationBarTitle({
            title: '成员',
          })
          break;
        case 'expendType':
          wx.setNavigationBarTitle({
            title: '支出类型',
          })
          break;
        case 'incomeType':
          wx.setNavigationBarTitle({
            title: '收入类型',
          })
          break;
        case 'accountType':
          wx.setNavigationBarTitle({
            title: '账户',
          })
          break;
        default:
          break;
      }
      that.setData({
        type: options.type
      })
    }
    wx.startPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    //防止刚进入app，还未拿到userInfo，所以这里用回调，确保能获取数据
    if (app.globalData.userInfo) {
      that.getTypeInfo()
      that.setData({
        user:app.globalData.userInfo
      })
    } else {
      app.userInfoReadyCallback = userInfo => {
        //用户信息保存到全局
        app.globalData.userInfo = userInfo
        that.getTypeInfo()
        that.setData({
          user:app.globalData.userInfo
        })
      }
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getTypeInfo: function () {
    var that = this
    wx.request({
      url: `${common.baseUrl()}/${that.data.type}/findByUser`,
      method: "GET",
      header: {
        // "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      // data: {
      //   userId: app.globalData.userInfo.id
      // },
      success(res) {
        wx.stopPullDownRefresh()
        console.log(res)
        if (res.data.flag) {
          //保存到当前页
          that.setData({
            typeList: res.data.data
          })
          //保存全局数据
          switch (that.data.type) {
            case 'member':
              app.globalData.memberList = res.data.data
              break;
            case 'expendType':
              app.globalData.expendTypeList = res.data.data
              break;
            case 'incomeType':
              app.globalData.incomeTypeList = res.data.data
              break;
            case 'accountType':
              app.globalData.accountTypeList = res.data.data
              break;

            default:
              break;
          }
        }
      },
      fail(res) {
        wx.stopPullDownRefresh()
        console.log(res)
      }
    })
  },
  typeClick: function (e) {
    var that = this
    var typeList = that.data.typeList
    var index = e.currentTarget.dataset.index
    var isOpen = true
    for (let i = 0; i < typeList.length; i++) {
      const type = typeList[i];
      if (type.isShow) {
        type.isShow = false
        isOpen = false
      }
    }
    that.setData({
      typeList: typeList
    })
    if(isOpen){
      if(app.globalData.userInfo.id!=typeList[index].userId){
        wx.showToast({
          title: '无法编辑',
          image: '/images/warning.png'
        })
        return
      }
      wx.navigateTo({
        url: `typeDetail/typeDetail?type=${that.data.type}&detail=${JSON.stringify(that.data.typeList[index])}`,
      })
    }
  },
  slideShow: function (e) {
    console.log(e.currentTarget.dataset.index)
    var that = this
    var typeList = that.data.typeList
    for (let i = 0; i < typeList.length; i++) {
      if (i != e.currentTarget.dataset.index) {
        if (typeList[i].isShow) {
          typeList[i].isShow = false
        }
      }else{
        typeList[i].isShow = true
      }
    }
    that.setData({
      typeList: typeList
    })
  },
  slideClick: function(e){
    var that = this
    var typeList = that.data.typeList
    var index = e.currentTarget.dataset.index
    if(app.globalData.userInfo.id!=typeList[index].userId){
      wx.showToast({
        title: '无法删除',
        image: '/images/warning.png'
      })
      return
    }
    wx.showLoading({
      title: '正在删除',
    })
    wx.request({
      url: `${common.baseUrl()}/${that.data.type}/remove/${typeList[index].id}`,
      method: "DELETE",
      header: {
        // "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": app.globalData.token
      },
      // data: {
      //   userId: app.globalData.userInfo.id
      // },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.flag) {
          wx.showToast({
            title: res.data.message
          })
          typeList.splice(index,1)
          that.setData({
            typeList:typeList
          })
        }else{
          wx.showToast({
            title: res.data.message,
            image: '/images/warning.png'
          })
        }
      },
      fail(res) {
        console.log(res)
      }
    })
  },
  createClick: function(e){
    var that = this
    wx.navigateTo({
      url: `typeDetail/typeDetail?type=${that.data.type}`,
    })
  }
})